/*
 * @Author: Setsuna
 * @Email: 601890383@qq.com
 * @FilePath: \src\pages\TodoList\OperatingItems\Table\index.tsx
 * @Date: 2021-12-28 14:14:44
 * @LastEditTime: 2022-01-10 11:48:00
 */

import React from 'react'
import TodoTable from '../../component/Table'
import { columnRenders, http, uaaApp, UdDrawer, UdModal } from '@/lib'
import todoApi from '@/apis/TodoList/index'
import ApproveForm from '../OperatingItemsForm'
import BatchForm from '../../QuotaFreeze/BatchForm'
import { message } from 'antd'
import {checkOptions} from '@/utils/methods'
export default class OperatingItemsTable extends TodoTable {
  private selectedRows: any = []
  private todoType = this.props.todoType
  private refresh = this.props.refresh
  constructor(props) {
    super(props)
    this.state = {
      columns: [
        ...this.commonColumns,
        {
          title: '操作',
          dataIndex: 'operate',
          width: 100,
          render: columnRenders.operate([
            {
              title: '查看详情',
              action: (text, record) => this.approve(record),
            },
          ]),
        },
      ],
      buttons: [],
      tableProps: {
        scroll: { x: true },
        rowSelection: {
          onChange: this.onSelectChange,
        },
      },
    }
  }
  componentDidMount() {
    if (this.props.todoType === 1 && uaaApp.canUse('todo_batch_approve'))
      this.setState({
        buttons: [
          // {
          //   type: 'primary',
          //   onClick: () => {},
          //   text: '批量审批',
          // },
        ],
      })
  }

  private onSelectChange = (selectedRowKeys, selectedRows) => {
    this.selectedRows = selectedRows
    if (this.props.todoType === 1 && this.selectedRows.length > 0 && uaaApp.canUse('todo_batch_approve')) {
      this.setState({
        buttons: [
          // {
          //   type: 'primary',
          //   onClick: this.handleBatchApprove.bind(this),
          //   text: '批量审批',
          // },
        ],
      })
    }
  }
  private handleBatchApprove = async () => {
    try {
      let result = checkOptions(this.selectedRows)
      if(result.size === 1){
        const approverRoles = await http.get(todoApi.getOperatingItemsNextTask, {
          params: { taskId: this.selectedRows[0].taskDefId },
        })
        let roleIds = approverRoles?.data?.[0]?.roleIds || []
        const taskId = this.selectedRows.map(({ taskDefId }) => taskDefId).join(',')
        UdModal.open({
          title: '审批',
          width: '30%',
          content: <BatchForm selectedRows={this.selectedRows} roleIds={roleIds} />,
          onOk: values => {
            let url = todoApi.completeBatch
            const userId = JSON.parse(sessionStorage.getItem('UaaSysInfo'))?.profile?.id
            const { isValid, comment, approveUserIds, roleIds } = values
            let params = { taskId, roleIds, userId, comment, approveUserIds,orderBy:this.props.orderBy  }
            if (isValid === 0) {
              params['workflowReqVos'] = {
                name: 'candidateUsers',
                userId: params['approveUserIds'],
              }
              delete params['approveUserIds']
            } else {
              url = todoApi.rejectBatch
            }
            return http.post(url, params).then(res => {
              message.success('操作成功')
              this.refresh(this.todoType)
            })
          },
        })
      }else{
        message.error('只能审批同一品牌公司')
      }
     
    } catch (err) {
      console.error(err)
    }
  }
  private approve(record) {
    UdDrawer.open({
      title: '审批详情',
      cancelText: null,
      okText: '审批',
      width: '40%',
      content: <ApproveForm record={record} todoType={this.props.todoType} refresh={this.props.refresh} />,
      footer: this.props.todoType === 1,
    })
  }
  public goDetail = row => {
    console.log(`row`, row)
    this.approve(row)
  }
}
